import {
  wxRequest
} from '../../utils/server.js';
const _wxRequest = new wxRequest();
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [],
    swiperIndex: 0,
    uploadUrl: wx.getStorageSync('uploadUrl'),
    currentItem: ''
  },
  swiperChange: function (e) {
    this.getcolorlist(e.detail.currentItemId)
    this.setData({
      swiperIndex: e.detail.current
    })
  },
  hanldImg: function (e) {
    var index = e.currentTarget.dataset.index
    var id = e.currentTarget.dataset.id
    this.getcolorlist(id)
    this.setData({
      swiperIndex: index
    })
  },
  onShow() {
    this.setData({
      swiperIndex: 0,
      itemColor: ''
    })
  },

  swiperItemChange: function (e) {
    var index = e.currentTarget.dataset.index
    var id = e.currentTarget.dataset.id
    this.setData({
      swiperIndex: index
    })
    this.getcolorlist(id)
  },

  onLoad(options) {
    if (options.typeId) {
      this.data.typeId = options.typeId
      wx.setStorageSync('devicesTypeId', this.data.typeId)
    }

    if (options.idx) {
      this.setData({
        swiperIndex: options.idx
      })
    }
    if (options.id) {
      this.setData({
        id: options.id
      })
      this.getcolorlist(options.id)
    }

    const eventChannel = this.getOpenerEventChannel();
    eventChannel.on && eventChannel.on('currentItem', data => {
      console.log('data ', data)
      this.setData({
        currentItem: data
      })
    })
  },

  onShow() {
    // this.getlist(); 
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          pixelRatio: res.pixelRatio,
          windowHeight: res.windowHeight,
          windowWidth: res.windowWidth
        })
      },
    })



  },


  //查询列表
  getlist: function () {
    var params = {
      level: 1,
      pageSize: 100,
      typeId: this.data.typeId
    }

    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/gds/protected/dyeColor/findList',
      method: 'POST',
      data: params
    }, (res) => {
      let list = res.list;
      if (!this.data.id) {
        this.getcolorlist(list[0].id)
      }

      this.setData({
        levelList: list
      });
    })
  },



  //子级颜色
  getcolorlist: function (pidId) {
    var params = {
      // level23:1,
      pageSize: 1000,
      typeId: this.data.typeId,
      bigId: pidId
    }
    console.log(params, 'params')
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/gds/protected/dyeColor/findList',
      method: 'POST',
      data: params
    }, (res) => {
      let list = res.list;
      console.log(res);
      var level2 = list.filter(item => item.level == 2)
      var level3 = list.filter(item => item.level == 3)
      console.log(2, level2);
      console.log(3, level3);
      // var newList=[]
      level2.map(item => {
        item.children = level3.filter(its => its.pid == item.id)
        //item.children = level3
      })




      console.log(level2, '颜色列表')
      this.setData({
        colorList: level2,
        itemColor: '',
        colorIndex: ''
      });
    })
  },



  thColor: function (e) {
    var item = e.currentTarget.dataset.item
    var index = e.currentTarget.dataset.index
    this.setData({
      itemColor: item,
      colorIndex: index,
    })
  },


  ComfItem: function () {
    if (!this.data.itemColor) {
      wx.showToast({
        icon: 'none',
        title: '请选择颜色',
      })
      return
    }
    //有模特图
    if (this.data.itemColor.model == 1) {
      wx.navigateTo({
        url: '/pages/choose_colors/model',
        success: (res) => {
          res.eventChannel.emit('colorItem', this.data.itemColor)
          res.eventChannel.emit('currentItem', this.data.currentItem)
        }
      })
    } else {
      wx.navigateTo({
        url: '/pages/colors_project/index'
      })
    }


  },




  //底部切换
  colorChange: function (e) {
    console.log(e.detail.current, 'colorChange')
  },


  //大图片切换
  colormaxChange: function (e) {
    console.log(e, 'colorChange')
    //tab选项居中                            
    this.setData({
      navScrollLeft: e.detail.current * 185 / 2,
      itemColor: this.data.colorList[e.detail.current]
    })
  },

  hanldLeft: function (e) {
    console.log(e, 'eee')
    var index = e.currentTarget.dataset.index;
    this.setData({
      colorIndex: index,
      itemColor: this.data.colorList[index]
    })
  },


  //选择颜色
  chooseColorItem: function (e) {
    console.log("e",e)
    var item = e.currentTarget.dataset.item;
    this.setData({
      itemColor: item
    })
    console.log("colorItem", this.data.itemColor)
    console.log("currentItem", this.data.currentItem)
    if (this.data.itemColor.model == 1) {
      wx.navigateTo({
        url: '/pages/choose_colors/model',
        success: (res) => {
          res.eventChannel.emit('colorItem', this.data.itemColor)
          res.eventChannel.emit('currentItem', this.data.currentItem)
        }
      })
    } else {
      wx.navigateTo({
        url: '/pages/colors_project/index'
      })
    }
  }






})